package com.dusk.tool
{
	import flash.utils.ByteArray;
	
	public class XMLTrans
	{
		
		
		private const NULL_TYPE:String = "undefined";
		private const BOOLEAN_TYPE:String = "Boolean";
		private const ARRAY_TYPE:String = "Array";
		private const STRING_TYPE:String = "String";
		private const OBJECT_TYPE:String = "Object";
		private const NUMBER_TYPE:String = "Number";
		private const XML_TYPE:String = "XML";
		
		public function XMLTrans()
		{
			super();
		}
		
		private var _xml:XML;
		
		public function objectToString(param1:Object):String
		{
			var _loc2_:* = null;
			var _loc3_:* = null;
			if (!param1)
			{
				return null;
			}
			if (param1 is Boolean)
			{
				_loc3_ = this.BOOLEAN_TYPE;
				_loc2_ = this.bolToXml(param1 as Boolean, null);
			}
			else
			{
				if (param1 is XML)
				{
					_loc2_ = param1 as XML;
					_loc2_.ignoreComments = true;
					return _loc2_.toString();
				}
				if (param1 is Array)
				{
					_loc3_ = this.ARRAY_TYPE;
					_loc2_ = this.arrToXml(param1 as Array, null);
				}
				else
				{
					if (param1 is String)
					{
						_loc3_ = this.STRING_TYPE;
						return param1 as String;
					}
					if (param1 is Number || param1 is int || param1 is uint)
					{
						_loc3_ = this.NUMBER_TYPE;
						return String(param1);
					}
					if (param1 is Object)
					{
						_loc3_ = this.OBJECT_TYPE;
						_loc2_ = this.objToXml(param1);
					}
				}
			}
			this.init2(_loc3_);
			this._xml.appendChild(_loc2_);
			this._xml.ignoreWhitespace = true;
			return this._xml.toString();
		}
		
		public function objToXml(param1:Object, param2:String = null):XML
		{
			var _loc3_:* = null;
			var _loc4_:* = null;
			var _loc5_:* = null;
			_loc3_ = <s/>;
			_loc3_.@type = this.OBJECT_TYPE;
			_loc3_.@name = param2;
			for (param2 in param1)
			{
				if ((_loc4_ = param1[param2]) is Boolean)
				{
					_loc5_ = this.bolToXml(_loc4_ as Boolean, param2);
					_loc3_.appendChild(_loc5_);
				}
				else if (_loc4_ is String)
				{
					_loc5_ = this.stringToXml(_loc4_ as String, param2);
					_loc3_.appendChild(_loc5_);
				}
				else if (_loc4_ is int || _loc4_ is uint || _loc4_ is Number)
				{
					_loc5_ = this.numToXml(_loc4_ as Number, param2);
					_loc3_.appendChild(_loc5_);
				}
				else if (_loc4_ is Array)
				{
					_loc5_ = this.arrToXml(_loc4_ as Array, param2);
					_loc3_.appendChild(_loc5_);
				}
				else if (_loc4_ is Object)
				{
					_loc5_ = this.objToXml(_loc4_, param2);
					_loc3_.appendChild(_loc5_);
				}
				else if (!_loc4_)
				{
					_loc5_ = this.nullToXml(null, param2);
					_loc3_.appendChild(_loc5_);
				}
			}
			return _loc3_;
		}
		
		public function decXml(param1:String):Object
		{
			var _loc2_:* = null;
			var _loc3_:Boolean = false;
			
			
			var _loc6_:Array = this.strToObj(param1 as String);
			var _loc7_:* = null;
			if (_loc6_[0] == "String")
			{
				param1 = _loc6_[1];
				_loc7_ = param1;
			}
			else if (_loc6_[0] == "XML")
			{
				_loc2_ = _loc6_[1];
				_loc7_ = _loc2_;
			}
			else if (_loc6_[0] == "Boolean")
			{
				_loc3_ = Boolean(_loc6_[1]);
				_loc7_ = _loc3_;
			}
			else if (_loc6_[0] == "Object")
			{
				_loc7_ = _loc6_[1];
			}
			else if (_loc6_[0] == "Array")
			{
				_loc7_ = _loc6_[1];
			}
			return _loc7_;
		}
		
		public function strToObj(param1:String):Array
		{
			var tmpStr:String = null;
			var str:String = param1;
			var obj:Object = null;
			var arr:Array = null;
			var bol:Boolean = false;
			var xml:XML = null;
			var xmlList:XML = null;
			var type:String = null;
			try
			{
				xml = new XML(str);
			} catch (error:Error)
			{
				tmpStr = str;
				tmpStr = this.urlEncodeGBK(tmpStr);
				try
				{
					xml = new XML(tmpStr);
				} catch (error:Error)
				{
					return [this.STRING_TYPE, str];
				}
			}
			if (xml.children() == null || xml.children() == undefined)
			{
				return [this.STRING_TYPE, str];
			}
			if (xml.@game4399 != "true")
			{
				return [this.XML_TYPE, xml];
			}
			if (xml.@type == this.OBJECT_TYPE)
			{
				xmlList = xml.children()[0];
				obj = this.xmlToObj(xmlList);
				type = this.OBJECT_TYPE;
				return [type, obj];
			}
			if (xml.@type == this.BOOLEAN_TYPE)
			{
				type = this.BOOLEAN_TYPE;
				xmlList = xml.children()[0];
				bol = Boolean(this.xmlToBoolean(xmlList));
				return [type, bol];
			}
			if (xml.@type == this.ARRAY_TYPE)
			{
				type = this.ARRAY_TYPE;
				xmlList = xml.children()[0];
				arr = this.xmlToArray(xmlList);
				return [type, arr];
			}
			return null;
		}
		
		private function nullToXml(param1:String, param2:String):XML
		{
			var xml:XML = <s/>;
			xml.@type = this.NULL_TYPE;
			xml.@name = param2;
			return xml;
		}
		
		private function stringToXml(param1:String, param2:String):XML
		{
			var xml:XML = new XML("<s>" + param1 + "</s>");
			xml.@type = this.STRING_TYPE;
			xml.@name = param2;
			return xml;
		}
		
		private function numToXml(param1:Number, param2:String):XML
		{
			var xml:XML = new XML("<s>" + param1.toString() + "</s>");
			xml.@type = this.NUMBER_TYPE;
			xml.@name = param2;
			return xml;
		}
		
		private function arrToXml(arr:Array, name:String):XML
		{
			var xml:XML = <s/>;
			xml.@type = this.ARRAY_TYPE;
			xml.@name = name;
			var _loc6_:*;
			var _loc7_:*;
			for (var idx:int = 0; idx < arr.length; idx++)
			{
				if ((_loc6_ = arr[idx]) is Boolean)
				{
					_loc7_ = this.bolToXml(_loc6_ as Boolean, null);
					xml.appendChild(_loc7_);
				}
				else if (_loc6_ is String)
				{
					_loc7_ = this.stringToXml(_loc6_ as String, null);
					xml.appendChild(_loc7_);
				}
				else if (_loc6_ is int || _loc6_ is uint || _loc6_ is Number)
				{
					_loc7_ = this.numToXml(_loc6_ as Number, null);
					xml.appendChild(_loc7_);
				}
				else if (_loc6_ is Array)
				{
					_loc7_ = this.arrToXml(_loc6_ as Array, null);
					xml.appendChild(_loc7_);
				}
				else if (_loc6_ is Object)
				{
					_loc7_ = this.objToXml(_loc6_, null);
					xml.appendChild(_loc7_);
				}
			}
			return xml;
		}
		
		private function bolToXml(param1:Boolean, param2:String):XML
		{
			var xml:* = null;
			if (param1)
			{
				xml = <s>true</s>;
			}
			else
			{
				xml = <s>false</s>;
			}
			xml.@type = this.BOOLEAN_TYPE;
			xml.@name = param2;
			return xml;
		}
		
		private function init2(param1:String):void
		{
			this._xml = <saveXml/>;
			this._xml.@type = param1;
			this._xml.@game4399 = true;
		}
		
		private function xmlToObj(param1:XML):Object
		{
			var itemList:Array = param1.children();
			var _loc9_:* = null;
			var obj:Object = {};
			for (var idx:int = 0; idx < itemList.length(); idx++)
			{
				var item:* = itemList[idx];
				var _type:String = item.@type;
				var _name:String = item.@name;
				if (_type == this.ARRAY_TYPE)
				{
					_loc9_ = this.xmlToArray(item) as Array;
				}
				if (_type == this.BOOLEAN_TYPE)
				{
					_loc9_ = this.xmlToBoolean(item) as Boolean;
				}
				if (_type == this.STRING_TYPE)
				{
					_loc9_ = this.xmlToString(item) as String;
				}
				if (_type == this.NUMBER_TYPE)
				{
					_loc9_ = this.xmlToNumber(item) as Number;
				}
				if (_type == this.OBJECT_TYPE)
				{
					_loc9_ = this.xmlToObj(item);
				}
				obj[_name] = _loc9_;
			}
			return obj;
		}
		
		private function xmlToArray(param1:XML):Array
		{
			var _loc2_:* = null;
			var _loc3_:int = 0;
			var _loc4_:int = 0;
			var _loc5_:* = null;
			var _loc6_:* = null;
			var _loc7_:* = null;
			var _loc8_:* = null;
			var _loc9_:* = null;
			_loc2_ = [];
			_loc4_ = int((_loc6_ = param1.children()).length());
			_loc3_ = 0;
			while (_loc3_ < _loc4_)
			{
				_loc8_ = (_loc7_ = _loc6_[_loc3_]).@type;
				_loc9_ = _loc7_.@name;
				if (_loc8_ == this.ARRAY_TYPE)
				{
					_loc5_ = this.xmlToArray(_loc7_) as Array;
				}
				if (_loc8_ == this.BOOLEAN_TYPE)
				{
					_loc5_ = this.xmlToBoolean(_loc7_) as Boolean;
				}
				if (_loc8_ == this.STRING_TYPE)
				{
					_loc5_ = this.xmlToString(_loc7_) as String;
				}
				if (_loc8_ == this.NUMBER_TYPE)
				{
					_loc5_ = this.xmlToNumber(_loc7_) as Number;
				}
				if (_loc8_ == this.OBJECT_TYPE)
				{
					_loc5_ = this.xmlToObj(_loc7_);
				}
				if (_loc8_ == this.NULL_TYPE)
				{
					_loc5_ = null;
				}
				_loc2_.push(_loc5_);
				_loc3_++;
			}
			return _loc2_;
		}
		
		private function xmlToNumber(param1:XML):Number
		{
			return Number(param1[0]);
		}
		
		private function xmlToBoolean(param1:XML):Boolean
		{
			var xml:Boolean = false;
			if (param1[0] == "false")
			{
				xml = false;
			}
			else if (param1[0] == "true")
			{
				xml = true;
			}
			return xml;
		}
		
		private function xmlToString(param1:XML):String
		{
			return String(param1[0]);
		}
		
		private function urlEncodeGBK(param1:String):String
		{
			var _loc3_:ByteArray = new ByteArray();
			_loc3_.writeMultiByte(param1, "gbk");
			_loc3_.position = 0;
			return _loc3_.readUTFBytes(_loc3_.length - 1);
		}
	}
}
